// import winston from "winston";
let logger;
if (process.server) {
  let winston = require("winston");
  logger = winston.createLogger({
    level: "info",
    format: winston.format.combine(
      winston.format.timestamp(),
      winston.format.json()
    ),
    transports: [
      new winston.transports.File({
        filename: "axios-logs.log",
        level: "info",
      }),
    ],
  });
}
export default function ({ app: { $axios, $cookies } }) {
  $axios.defaults.baseURL = process.env.baseUrl;
  $axios.defaults.timeout = 30000;
  $axios.interceptors.request.use((config) => {
    // console.log("config", config);
    if (process.server) {
      logger.info("Request", {
        method: config.method,
        url: config.url,
        headers: config.headers,
        data: config.data,
      });
    }

    // config.headers["X-Token"] = $cookies.get("token") || "";
    // config.headers["X-Device-Id"] = $cookies.get("clientId") || "";
    // config.headers["X-Uid"] = $cookies.get("userId") || "";
    return config;
  });
  $axios.interceptors.response.use((response) => {
    if (/^[4|5]/.test(response.status)) {
      return Promise.reject(response.statusText);
    }
    if (process.server) {
      logger.info("Response", {
        status: response.status,
        statusText: response.statusText,
        headers: response.headers,
        data: response.data,
      });
    }

    return response.data;
  });
}
